package fhj.swd09.resteasy;

import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

@Path("/chuckfacts")
public class FactsResource 
{
	//Property fakeDB:Map<Integer, Fact>
	private Map<Integer, Fact> fakeDB = new ConcurrentHashMap<Integer, Fact>();
	public Map<Integer, Fact> getFakeDB() 
	{
		return fakeDB;
	}
	public void setFakeDB(Map<Integer, Fact> fakeDB) 
	{
		this.fakeDB = fakeDB;
	}
	
	//property id:Integer
	private Integer id;
	public Integer getId() 
	{
		return id;
	}
	public void setId(Integer id) 
	{
		this.id = id;
	}
		
	//Constructor
	public FactsResource()
	{
		this.setId(0);
	
		Fact f1 = new Fact();
		Fact f2 = new Fact();
		Fact f3 = new Fact();
		f1.setFavourite(false);
		f1.setText("The best thing about waking up is to know that Cuck Norris didn't kill you in your sleep.");
		f2.setFavourite(true);
		f2.setText("All arrays Chuck Norris declares are of infinite size because Chuck Norris knows no bounds.");
		f3.setFavourite(false);
		f3.setText("Chuck Norris can solve the 'Towers of Hanoi' in one move.");
		
		this.fakeDB.put(this.getId(), f1);
		this.fakeDB.put(this.getNextId(), f2);
		this.fakeDB.put(this.getNextId(), f3);
	}
   
	//GET-Methods
    @GET 
    @Produces("text/html")
    public String getAllFacts() 
    {
        StringBuilder output = new StringBuilder();
        
        output.append("<html><body>");
        for (int i = 0; i < fakeDB.size(); i++)
        {
        	output.append("id: " + i + "</br>");
        	output.append(fakeDB.get(i).toHtml());
        }
        output.append("</body></html>");
        
        return output.toString();
    }
    
    @GET 
    @Path("{id}")
    @Produces("text/html")
    public String getFact(@PathParam("id") int id) 
    {
        StringBuilder output = new StringBuilder();
        
        output.append("<html><body>");
        output.append("id: " + id + "</br>");
        output.append(fakeDB.get(id).toHtml());
        output.append("</body></html>");
        
        return output.toString();
    }
    
    //POST-Method
    @POST
    @Consumes("text/plain")
    public Response createFact(String input) 
    {
       Fact fact = readFact(input);
      
       fakeDB.put(this.getNextId(), fact);
       System.out.println("Created fact " + this.getId());
      
       return Response.created(URI.create("/chuckfacts/" + this.getId())).build();
    }
	    
    //PUT-Methods
    @PUT
    @Path("{id}")
    @Consumes("text/plain")
    public void updateCustomer(@PathParam("id") int id, String input) 
    {
       Fact update = readFact(input);
       Fact current = fakeDB.get(id);
       
       if (current == null) 
    	   throw new WebApplicationException(Response.Status.NOT_FOUND);

       current.setText(update.getText());
       current.setFavourite(update.isFavourite());
       
    }
    
    //Methods
    private Fact readFact(String input) 
    {
		Fact f = new Fact();
    	int delimiter = input.indexOf(';');
		   
		f.setText(input.substring(0, delimiter));
		if (input.substring(delimiter+1).equalsIgnoreCase("true"))
		{
			f.setFavourite(true);
		}
		else
		{
			f.setFavourite(false);
		}
		   
		return f;
	}
    
    public Integer getNextId() 
	{
		this.id++;
		return id;
	}
}